/**
 * Created by kitabi on 2017/6/20.
 */
/**
 * Created by kitabi on 2017/6/16.
 */
var classify={

};

var $btnStaffConfirm= $("#btnStaffConfirm");
var $staffDialog= $("#staffDialog");
/**
 * 获取用户数据
 */
classify.queryList= function(){


    $("#mainTable").bootstrapTable({

        method:'POST',
        dataType:'json',
        contentType: "application/x-www-form-urlencoded",
        cache: false,
        striped: true,                              //是否显示行间隔色
        sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
        url:"/cash/qryCashList",
        // height: $(window).height() - 110,
        // width:$(window).width(),
        // showColumns:true,
        pagination:true,
        // queryParams : queryParams,
        minimumCountColumns:2,
        pageNumber:1,                       //初始化加载第一页，默认第一页
        pageSize: 10,                       //每页的记录行数（*）
        pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
        uniqueId: "id",                     //每一行的唯一标识，一般为主键列
        showExport: true,
        exportDataType: 'all',


        paginationPreText:"<",
        paginationNextText:">",
        queryParams: function(params) {

            var userSearch= $("#userSearch").val();

            var userDateStart=$("#userDateStart").val();
            var userDateEnd=$("#userDateEnd").val();
            return {
                cPage: (params.offset/params.limit)+1,
                pSize: params.limit,

                status:$("#userStatus").val(),
                name:userSearch,
                start:userDateStart,
                end:userDateEnd,

            };
        },
        columns: [
            {
                field: 'state',
                checkbox: true,
                align: 'center',
                valign: 'middle'
            },{
                field: 'id',
                title: 'ID'
            },{
                field: 'nick_name',
                title: '用户名称'
            },{
                field: 'cash_num',
                title: '提现现金',
                formatter: function(value,row,index){

                    return parseFloat(value/100);
                }
            },{
                field: 'type_name',
                title: '提现渠道'
            },{
                field: 'account_num',
                title: '提现账号'
            },{
                field: 'account_name',
                title: '提现姓名'
            },{
                field: 'create_time',
                title: '申请时间'
            },{
                field: 'deal_time',
                title: '处理时间'
            }, {

                field: 'status',
                title: '状态',
                align: 'center',

                formatter: function(value,row,index){
                    //value：当前field的值，即id
                    //row：当前行的数据
                    var status= row.status;
                    var name="";
                    if(status ==0){
                        name="未处理";
                    }else if(status ==1){
                        name="已到账";
                    }else if(status ==-1){
                        name="到账失败";
                    }else if(status ==2){
                        name="处理中";
                    }else if(status==3){
                        name="取消打款";
                    }
                    return  name;
                }
            }, {

                field: 'status',
                title: '操作',
                align: 'center',

                formatter: function(value,row,index){
                    //value：当前field的值，即id
                    //row：当前行的数据
                    var id= row.id;
                    var status= row.status;

                    var add="";
                    var e="";
                    if(status==0 || status==-1){
                        add='<button type="button" onclick="updateStatus('+id+',\'2\')" style="margin-right: 5px;" class="btn btn-sm btn-primary btn-trans">审核打款</button>';

                        e='<button type="button" onclick="updateStatus('+id+',\'3\')" style="margin-right: 5px;" class="btn btn-sm btn-danger btn-trans">拒绝</button>';
                    }else if(status == 2){
                        add='<button type="button" onclick="updateStatus('+id+',\'1\')" style="margin-right: 5px;" class="btn btn-sm btn-primary btn-trans">打款成功</button>';
                        e='<button type="button" onclick="updateStatus('+id+',\'3\')" style="margin-right: 5px;" class="btn btn-sm btn-danger btn-trans">取消打款</button>';

                    }else if(status == 3){
                        add='<button type="button" onclick="updateStatus('+id+',\'2\')" style="margin-right: 5px;" class="btn btn-sm btn-primary btn-trans">审核打款</button>';

                    }

                    return  add+e;
                }
            }]

    });

}

function updateStatus(id ,type){
    var param={
        id: id,
        status: type,

    }
    Ajax.ajax({
        url: "/cash/updateStatus",
        data: param,
        type: "POST",
        dataType: 'json',
        success: function (res) {
            if (res.resultCode == 0) {
                $("#mainTable").bootstrapTable('refresh');
            } else {
                alert("操作失败");
            }
        },
        error: function(){
            alert("操作失败");
        }
    });
}

function doAll(id ,type){
    var select= $("#mainTable").bootstrapTable('getSelections');

    if(select.length ==0){
        alert("请先勾选一行");
        return;
    }

    var idlist= "";
    for(var i=0;i<select.length;i++){
        var temp = select[i];
        if(temp.status != -1 ||temp.status != 0||temp.status != 3){


            alert("请注意，提现单号: "+temp.id+"  状态为处理中或打款成功，不能修改打款状态！");
            return ;
        }

        if(i==0){
            idlist+=temp.id;
        }else {
            idlist += "," + temp.id;
        }
    }
    var param={
        id: id,
        status: 2

    }
    Ajax.ajax({
        url: "/cash/updateStatus",
        data: param,
        type: "POST",
        dataType: 'json',
        success: function (res) {
            if (res.resultCode == 0) {
                $("#mainTable").bootstrapTable('refresh');
            } else {
                alert("操作失败");
            }
        },
        error: function(){
            alert("操作失败");
        }
    });
}


$(function(){
    //开始\结束 日期选择
    $("#userDateStart").datepicker({
        format: 'yyyy-mm-dd ',  language: 'zh-CN',                 autoclose: true,                 todayHighlight: true
    });
    $("#userDateEnd").datepicker({
        format: 'yyyy-mm-dd ',  language: 'zh-CN',                 autoclose: true,                 todayHighlight: true
    });

    classify.queryList();


    $("#userSearchBtn").click(function(){
        $("#mainTable").bootstrapTable('refresh');
    });
});